package com.shop.operator.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.domain.PageResult;
import com.shop.model.ContentCategory;
import com.shop.operator.service.ContentCategoryService;


@Controller
public class ContentCategoryAction {

	@Resource
	private ContentCategoryService contentCategoryService;
	
	
	/**
	 * http://localhost:8080/Shopping/returncontentcategory.action
	 * 跳转到广告类表页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/returncontentcategory")
	public String returncontentcategory() throws Exception{
		
		return "forward:/WEB-INF/operator/admin/content_category.jsp";
	}
	
	
	/**
	 * http://localhost:8080/Shopping/findByLike.action
	 * 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findByLike")
	public PageResult findByLike(@RequestBody ContentCategory contentCategory,Integer page,Integer size) throws Exception{
		
		System.out.println("查询所有的广告分类");
		return this.contentCategoryService.findByLike(contentCategory, page, size);
		
		
	}
	
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findById")
	public ContentCategory findById(Long id) throws Exception{
		
		System.out.println("根据id查找"+id);
		return this.contentCategoryService.findOneById(id);
	}
	/**
	 * 更新商品
	 * @param contentCategory
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/updatename")
	public Map<String,String> update(@RequestBody ContentCategory contentCategory) throws Exception{
				boolean bool = false;
				//获取所有的广告名
				List<String> listNames = this.contentCategoryService.findAllNames();
				
				//前端传递过来的广告名不为空    判断添加的广告名不能重复
				if(contentCategory.getName()!=null){
					if(listNames.contains(contentCategory.getName())){//数据库中已经存在了该广告名
						bool=false;
						System.out.println("返回值："+bool);
					}else{
						 bool = this.contentCategoryService.update(contentCategory);
					}
				}
		Map<String,String> map = new HashMap<String, String>();
		
		if(bool){
			map.put("success", "true");
		}else
		{
			map.put("message", "更新失败,该广告名已经存在");
		}
		return map;
	}
	
	/**
	 * 添加商品
	 * @param contentCategory
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,String> add(@RequestBody ContentCategory contentCategory) throws Exception{
		boolean bool = false;
		//获取所有的广告名
		List<String> listNames = this.contentCategoryService.findAllNames();
		
		
		//前端传递过来的广告名不为空    判断添加的广告名不能重复
		if(contentCategory.getName()!=null){
			if(listNames.contains(contentCategory.getName())){//数据库中已经存在了该广告名
				bool=false;
				System.out.println("返回值："+bool);
			}else{
				bool = this.contentCategoryService.add(contentCategory);
			}
		}
		Map<String,String> map = new HashMap<String, String>();
		
		if(bool){
			map.put("success", "true");
		}else
		{
			map.put("message", "更新失败,该广告名已经存在");
		}
		return map;
	}
    /**
     * 删除商品
     */
	
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String,String> delete(Long[] ids) throws Exception{
		boolean bool = this.contentCategoryService.delete(ids);
		Map<String,String> map = new HashMap<String, String>();
		
		if(bool){
			map.put("success", "true");
		}else
		{
			map.put("message", "删除失败");
		}
		return map;
	}
	
	/**
	 * 查询所有的广告
	 * http://localhost:8080/Shopping/findAll.action
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/findAll")
	public List<ContentCategory> findAll() throws Exception{
		
		System.out.println("查询所有的广告");
	return	this.contentCategoryService.findAll();
	
	}
	
	
}
